package com.seed.core.query;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryTable;
import com.seed.core.column.Column;
import com.seed.core.enumeration.PropertyType;
import com.seed.core.pojo.Join;
import com.seed.core.pojo.seed.HasManys;
import com.seed.core.pojo.seed.Relation;
import com.seed.core.pojo.seed.Seed;
import com.seed.core.query.pojo.Searched;
import com.seed.core.util.ConditionUtil;
import com.seed.util.StrKit;
import com.seed.util.SeedKit;

import java.util.*;

public class SeedSearched {
    private final List<QueryCondition> wheres = new ArrayList<>(); // 条件对象
    private final Map<String, Join> joins = new LinkedHashMap<>(); // 表关联对象
    private final Map<String, SeedInCondition> inQuerys = new LinkedHashMap<>();
    private final Map<String, Searched> fillSearcheds = new HashMap<>(); // fill对象使用

    public SeedSearched(Seed selfSeed, Map<String, Searched> searcheds) {
        init(null, selfSeed, searcheds);
    }

    private void init(String basePath, Seed selfSeed, Map<String, Searched> searcheds) {
        String selfTableAlias = StrKit.selfTableAlias(basePath);
        QueryTable selfTable = new QueryTable(selfSeed.getTableName()).as(selfTableAlias);
        for(Map.Entry<String, Searched> entry: searcheds.entrySet()) {
            String[] items = StrKit.cutInTwo(StrKit.cutInTwo(entry.getKey(), '=', false)[0], '_', true);
            String crossOrColumn = items[0];
            String op = items[1];
            String finalPath = StrKit.concat(basePath, crossOrColumn);
            Searched value = entry.getValue();
            PropertyType propertyType = value.getType();
            switch (propertyType) {
                case none -> {
                    Column nameColumn = SeedKit.getNameColumn(selfSeed);
                    String val = value.getValue();
                    wheres.add(ConditionUtil.condition(selfTable, nameColumn.getName(), StrUtil.isBlank(op) ? nameColumn.getDefaultOp().name() : op, val));
                }
                case asterisk -> {
                    Map<String, Column> columns = selfSeed.getColumns();
                    for (Column column: columns.values()) {
                        String columName = column.getName();
                        String val = value.getValue();
                        wheres.add(ConditionUtil.condition(selfTable, columName, column.getDefaultOp().name(), val));
                    }
                }
                case column -> {
                    String val = value.getValue();
                    Column column = (Column) value.getProperty();
                    op = StrUtil.isBlank(op) ? column.getDefaultOp().name() : op;
                    wheres.add(ConditionUtil.condition(selfTable, crossOrColumn, op, val));
                }
                case hasOne, belongsTo -> {
                    Relation relation = (Relation) value.getProperty();
                    Seed targetSeed = relation.targetSeed();
                    String targetTableAlias = StrUtil.toCamelCase(finalPath);
                    QueryTable targetTable = new QueryTable(targetSeed.getTableName()).as(targetTableAlias);

                    QueryCondition on = relation.getQueryCondition(basePath, crossOrColumn);
                    joins.putIfAbsent(finalPath, new Join(targetTable, on));
                    init(finalPath, targetSeed, value);
                }
                default -> {
                    Relation relation = (Relation) value.getProperty();
                    Seed targetSeed = relation.targetSeed();
                    inQuerys.put(finalPath, new SeedInCondition(selfSeed, basePath, crossOrColumn, (HasManys) relation, targetSeed, value));
                    fillSearcheds.put(finalPath, value);
                }
            }
        }
    }

    public List<QueryCondition> getWheres() {
        return wheres;
    }

    public Map<String, Join> getJoins() {
        return joins;
    }

    public Map<String, SeedInCondition> getInQuerys() {
        return inQuerys;
    }

    public Searched getFillSearched(String key) {
        return fillSearcheds.get(key);
    }

    public Map<String, Searched> getFillSearcheds() {
        return fillSearcheds;
    }
}
